package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class InfoPanel extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tf;
	private DefaultTableModel dtm;
	private JTable table;
	private JTextArea chat;
	private JLabel label2;

	public InfoPanel() {
		setBackground(Color.yellow);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel top = new JPanel();
		top.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		JLabel label1 = new JLabel();
		label1.setIcon(new ImageIcon(getClass().getResource("../image/photo1.png")));
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(20, 0, 10, 0);
		top.add(label1, gbc);
		label2 = new JLabel("玩家信息");
		gbc.gridy = 1;
		top.add(label2, gbc);
		add(top);

		JPanel middle = new JPanel();
		middle.setLayout(new BorderLayout());
		dtm = new DefaultTableModel(new Object[][] {}, new String[] { "昵称", "IP", "联机时间" });
		table = new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane sp = new JScrollPane(table);
		sp.setPreferredSize(new Dimension(230, 200));
		middle.add(sp);
		add(middle);

		JPanel bottom = new JPanel();
		bottom.setLayout(new BorderLayout());
		chat = new JTextArea();
		chat.setPreferredSize(new Dimension(200, 500));
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		tf = new JTextField();
		JButton send = new JButton("发送");
		send.addActionListener(this);
		panel.add(tf);
		panel.add(send, BorderLayout.EAST);
		bottom.add(chat);
		bottom.add(panel, BorderLayout.SOUTH);
		add(bottom);

	}

	@Override
	public void update(Observable o, Object arg1) {
		if (arg1 instanceof User) {
			User user = (User) arg1;
			if (o.equals(user)) {
				label2.setText(user.getName());
				dtm.addRow(new String[] { user.getName(), user.getIp(), user.getLoginName() });
				repaint();
			} else {
				dtm.addRow(new String[] { user.getName(), user.getIp(), user.getLoginName() });
				repaint();
			}
		} else if (arg1 instanceof String) {
			String str = (String) arg1;
			chat.append(str);
			repaint();
		} else if (arg1 instanceof Integer) {
			int ist = (int) arg1;
			if (ist == StateCode.RESET) {
				chat.setText("");
				dtm.setRowCount(0);
				repaint();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object omes = e.getActionCommand();
		if ((omes.equals("发送")) && (tf.getText() != (""))) {
			String sms = SocketUtil.getUser().getName() + ":" + tf.getText() + "\n";
			chat.append(sms);
			SocketUtil.sendMessage(sms);
			tf.setText("");
			repaint();
		}

	}
}
